package com.sctvcloud.bazhong.ui.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.canyinghao.canrefresh.other.CanRecyclerViewHeaderFooter;
import com.ruihang.generalibrary.utils.DensityUtil;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.base.BaseDetailActivity;
import com.sctvcloud.bazhong.application.Constances;
import com.sctvcloud.bazhong.beans.FComment;
import com.sctvcloud.bazhong.beans.NewsItem;
import com.sctvcloud.bazhong.http.AbsListNetCallback;
import com.sctvcloud.bazhong.http.NetUtils;
import com.sctvcloud.bazhong.ui.adapter.AnchorRelateNewsAdapter;
import com.sctvcloud.bazhong.ui.util.RBaseItemDecoration;
import com.sctvcloud.bazhong.ui.utils.TitleUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuxipan on 2018-02-07.
 * 主播关联的新闻页面
 *
 */

public class AnchorRelateNewsActivity extends BaseDetailActivity implements CanRefreshLayout.OnRefreshListener
        , CanRecyclerViewHeaderFooter.OnLoadMoreListener {

    @BindView(R.id.comment_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView comment_list;
    @BindView(R.id.comment_footer)
    protected CanRecyclerViewHeaderFooter footer;
    @BindView(R.id.comment_foot_pb)
    protected ProgressBar pb;
    @BindView(R.id.comment_foot_loadmore)
    protected View allLoaded;
    private LinearLayoutManager manager;
    private String anchorId;
    private AnchorRelateNewsAdapter adapter;
    private int indexNumber = 0;
    private int pageSize = 10;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        initView();
    }

    private void initView() {
        refreshLayout.setOnRefreshListener(this);
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        comment_list.setItemAnimator(new DefaultItemAnimator());
        comment_list.setLayoutManager(manager);
        comment_list.addItemDecoration(new RBaseItemDecoration(DensityUtil.dip2px(this, 1),
                ContextCompat.getColor(this, R.color.colorItemListDivider)));
        anchorId = getIntent().getStringExtra(EX_ID);
        footer.attachTo(comment_list, false);
        footer.setLoadMoreListener(this);
        footer.setVisibility(View.GONE);
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_anchor_relate_news);
        ButterKnife.bind(this);
    }

    @Override
    public void onLoadMore() {
        indexNumber++;
        getData();
    }

    @Override
    public void onChildAttached(View v) {

    }

    @Override
    public void onRefresh() {
        indexNumber = 0;
        getData();
    }

    /**
     * 主播相关的新闻请求
     */
    private void getData() {
        if (TextUtils.isEmpty(anchorId)) {
            toast("主播ID不能为空");
            return;
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("anchorId", anchorId);
        param.put("count", pageSize);
        param.put("index", indexNumber);
        param.put("siteNumber", Constances.SITE_ID);
        NetUtils.getNetAdapter().getAnchorRelateNewsList(getOwnerName(), param, new AbsListNetCallback<NewsItem>(NewsItem.class) {
            @Override
            public void onSuc(List<NewsItem> newsItems) {
                if (ListUtils.isListValued(newsItems) && newsItems.size() >= pageSize) {
                    if (!footer.isLoadEnable()) {
                        footer.setLoadEnable(true);
                        pb.setVisibility(View.VISIBLE);
                        allLoaded.setVisibility(View.GONE);
                    }
                } else {
                    footer.setLoadEnable(false);
                    pb.setVisibility(View.GONE);
                    allLoaded.setVisibility(View.VISIBLE);
                }
                initInfo(newsItems);
                onNetFinish();
            }
        });

    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        } else {
            footer.loadMoreComplete();
        }
    }

    /**
     * 主播相关新闻列表数据刷新
     * @param datas
     */
    private void initInfo(List<NewsItem> datas) {
        if (adapter == null) {
            adapter = new AnchorRelateNewsAdapter(this, datas);
            comment_list.setAdapter(adapter);
        } else {
            if (indexNumber == 0) {
                adapter.setData(datas);
            } else {
                adapter.addDatas(datas);
            }
        }
        if (adapter.getItemCount() == 0) {
            footer.setVisibility(View.GONE);
        } else {
            footer.setVisibility(View.VISIBLE);
        }
    }
}
